package com.windward.vii.web.controller;

import com.windward.vii.common.JsonResult;
import com.windward.vii.common.ResultCode;
import com.windward.vii.service.RedPacketService;
import com.windward.vii.service.UserTokenService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by tiansj on 2017/8/26.
 */
@Controller
public class RedPacketController {

    @Resource
    UserTokenService userTokenService;
    @Resource
    RedPacketService redPacketService;

    @RequestMapping(value = "v1/api1/redpacket/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getRedPacketList() {
        return redPacketService.getRedPacketList();
    }

    /**
     * 用户发红包给主播
     * @param anchorUid
     * @param token
     * @return
     */
    @RequestMapping(value = "v1/api1/user/redpacket/send", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult sendRedPacket(@RequestParam Long anchorUid,
                                    @RequestParam Long chatId,
                                    @RequestParam Long redPacketId,
                                    @RequestHeader(value="token") String token) {
        if(anchorUid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "targetUid错误");
        }
        if(chatId <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "chatId错误");
        }
        if(redPacketId <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "redPacketId错误");
        }
        Long uid = userTokenService.getUidByToken(token);
        return redPacketService.sendGift(uid, anchorUid, chatId, redPacketId);
    }

}
